﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;

namespace WeChatApp.Helper
{
    public static class DBHelper
    {
        public static SqlSugarClient InitialSqlSugarClient()
        {
            return new SqlSugarClient(new ConnectionConfig { ConnectionString=RepositoryConst.DefaultConnectString,DbType=SqlSugar.DbType.MySql,IsAutoCloseConnection=true});
        }
        public static SqlSugarClient InitialSqlSugarClient(string connectString)
        {
            return new SqlSugarClient(new ConnectionConfig { ConnectionString=connectString,DbType=SqlSugar.DbType.MySql,IsAutoCloseConnection=true});
        }
        public static SqlSugarClient DbModel
        {
            get { return InitialSqlSugarClient(); }
        }
        public static ISugarQueryable<T> DataSorting<T>(this ISugarQueryable<T> source,string sortExpression,string sortDirection)
        {
            if (string.IsNullOrEmpty(sortExpression))
            {
                return source;
            }
            if (sortDirection.ToUpper().Trim() == "ASC")
            {
                source = source.OrderBy(sortExpression + " asc");
            }
            else if (sortDirection.ToUpper().Trim() == "DESC")
            {
                source = source.OrderBy(sortExpression + " desc");
            }
            return source;
        }
        public static ISugarQueryable<T> DataSorting<T>(this ISugarQueryable<T> source, string sortExpression, bool IsSortDesc)
        {
            return DataSorting(source, sortExpression, IsSortDesc ? "DESC" : "ASC");
        }
        public static ISugarQueryable<T> DataPaging<T>(this ISugarQueryable<T> source, int pageNumber, int pageSize)
        {
            pageNumber = (pageNumber == 0) ? 0 : pageNumber - 1;

            pageSize = (pageSize == 0) ? 20 : pageSize;

            if (pageNumber * pageSize == 0)
            {
                return source.Take(pageSize);
            }
            else
            {
                return source.Skip(pageNumber * pageSize).Take(pageSize);
            }
        }
        public static ISugarQueryable<T> SortingAndPaging<T>(this ISugarQueryable<T> source, string sortExpression, string sortDirection, int pageNumber, int pageSize)
        {
            ISugarQueryable<T> query = DataSorting<T>(source, sortExpression, sortDirection);
            return DataPaging(query, pageNumber, pageSize);
        }

        //public static ISugarQueryable<T> SortingAndPaging<T>(this ISugarQueryable<T> source, PageInfo pageInfo)
        //{
        //    string sortExpression = string.IsNullOrEmpty(pageInfo.SortColumn) ? "CREATETIME" : pageInfo.SortColumn;

        //    if (!string.IsNullOrEmpty(pageInfo.SortColumnPrefix))
        //    {
        //        sortExpression = pageInfo.SortColumnPrefix + "." + sortExpression;
        //    }

        //    //pageInfo.PageSize = pageInfo.PageSize == 0 ? 20 : pageInfo.PageSize;
        //    //pageInfo.PageIndex = pageInfo.PageIndex == 0 ? 20 : pageInfo.PageIndex;
        //    ISugarQueryable<T> query = DataSorting<T>(source, sortExpression, pageInfo.IsSortDesc);
        //    pageInfo.TotalCount = query.Count();

        //    return DataPaging(query, pageInfo.PageIndex, pageInfo.PageSize);
        //}
    }
}
